home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Visual Basic Source Code
/
Visual Basic Source Code.iso
/
vbsource
/
cenvid
/
touch.cmm
< prev
next >
Wrap
Text File
|
1995-10-13
|
6KB
|
212 lines
/*
* Touch.cmm
*
* Update a file's date and time to the current time. Also, set
* the archive bit.
*/
#include "Netware.lib"
usage()
{
printf("Use the touch command to update one or more file(s) to have\n");
printf("their timestamp set to the current time and their archive bit\n");
printf("set.\n");
printf("Syntax:\n");
printf(" TOUCH [drive][path][filename]\n");
printf("where:\n");
printf(" drive/path/filename Specifies the file to be touched.\n");
exit(EXIT_FAILURE);
}
/* ---------------------------------------------------------------------- */
/*
* Some DOS functions
*/
SetFileAttributes(pFileName,pAttributes)
{
lReg.ah = 0x43;
lReg.al = 1;
lReg.cx = pAttributes;
if !defined(_DOS32_)
lReg.ds = segment(pFileName), lReg.dx = offset(pFileName);
else
lReg.dx = pointer(pFileName);
return interrupt(0x21,lReg);
}
SetFileDateAndTime(pFileName,pTime)
{
lSuccess = False;
// Open file to get a handle
lReg.ah = 0x3D;
lReg.al = 0x42;
if !defined(_DOS32_)
lReg.ds = segment(pFileName), lReg.dx = offset(pFileName);
else
lReg.dx = pointer(pFileName);
if ( interrupt(0x21,lReg,lRegout) ) {
lHandle = lRegout.ax;
// write date to file
undefine(lReg);
lReg.ah = 0x57;
lReg.al = 1;
lReg.bx = lHandle;
lTm = localtime(pTime);
lReg.cx = (lTm.tm_sec / 2)
| (lTm.tm_min << 5)
| (lTm.tm_hour << 11);
lReg.dx = lTm.tm_mday
| ((lTm.tm_mon+1) << 5)
| ((lTm.tm_year-80) << 9);
lSuccess = interrupt(0x21,lReg);
// close file
undefine(lReg);
lReg.ah = 0x3E;
lReg.bx = lHandle;
interrupt(0x21,lReg);
}
return lSuccess;
}
DOSTimeFromCalendar(time)
{
dos = DOSTimeStructFromCalendar(time);
return dos.time<<16 | dos.date;
}
DOSTimeStructFromCalendar(time)
{
tm = localtime(time);
dos.time = (tm.tm_sec/2) | (tm.tm_min<<5) | (tm.tm_hour<<11);
dos.date = tm.tm_mday | ((tm.tm_mon+1)<<5) | ((tm.tm_year-80)<<9);
return dos;
}
GMDOSTimeStructFromCalendar(time)
{
tm = gmtime(time);
dos.time = (tm.tm_sec/2) | (tm.tm_min<<5) | (tm.tm_hour<<11);
dos.date = tm.tm_mday | ((tm.tm_mon+1)<<5) | ((tm.tm_year-80)<<9);
return dos;
}
/* ---------------------------------------------------------------------- */
dir[0] = "";
it = 1;
parse_command_line(argc,argv)
{
for( i=1;i<argc;i++ )
{
if( argv[i][0]=='-' || argv[i][0]=='/' )
{
usage();
} else {
if( it )
{
dir[0] = argv[i]; it = 0;
} else {
dir[GetArraySpan(dir)+1] = argv[i];
}
}
}
}
/* ---------------------------------------------------------------------- */
touch_file(dir)
{
curtime = time();
if( defined(_NWNLM_) )
{
tempcreate[0] = DOSTimeFromCalendar(curtime);
tempaccess[0] = DOSTimeFromCalendar(curtime);
tempdate[0] = DOSTimeFromCalendar(curtime);
tempbackup[0] = DOSTimeFromCalendar(curtime);
code = NLMLink("SetFileInfo", dir.name, 0x06, dir.attrib | _A_ARCH,
tempcreate,
tempaccess,
tempdate,
tempbackup,
dir.uid);
if( code )
printf("\nError setting attributes for file %s\n",dir.name);
return;
}
if( defined(_DOS_) || defined(_DOS32_) || defined(_WINDOWS_) )
{
SetFileDateAndTime(dir.name,curtime);
SetFileAttributes(dir.name,dir.attrib | _A_ARCH);
return;
}
if( defined(_NTCON_) || defined(_NTWIN_) )
{
#define GENERIC_WRITE 0x40000000
#define FILE_SHARE_READ 0x01
#define FILE_SHARE_WRITE 0x02
#define OPEN_EXISTING 3
#define INVALID_HANDLE_VALUE -1
newbuf = ""; SetArraySpan(newbuf,4);
DynamicLink("KERNEL32","SetFileAttributesA",STDCALL,dir.name,
dir.attrib | _A_ARCH);
if( (handle = DynamicLink("KERNEL32","CreateFileA",STDCALL,dir.name,
GENERIC_WRITE,FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,OPEN_EXISTING,0,NULL))
!=INVALID_HANDLE_VALUE)
{
dos = GMDOSTimeStructFromCalendar(time());
DynamicLink("KERNEL32","DosDateTimeToFileTime",STDCALL,
dos.date,dos.time,newbuf);
if( !DynamicLink("KERNEL32","SetFileTime",STDCALL,handle,newbuf,
newbuf,newbuf) )
{
err = DynamicLink("KERNEL32","GetLastError",STDCALL);
printf("Unable to set new file time.\n",err);
}
if( !DynamicLink("KERNEL32","CloseHandle",STDCALL,handle) )
printf("Unable to close the handle.\n");
} else {
printf("Unable to open the file to touch it.\n");
}
return;
}
if( defined(_OS2_) )
{
printf("OS/2 not yet supported.\n");
return;
}
}
main(argc,argv)
{
if( defined(_OS2_) )
{
printf("This script does not yet support OS/2.\n");
exit(EXIT_FAILURE);
}
parse_command_line(argc,argv);
if( it ) Usage();
for( i=0;it==0 && i<=GetArraySpan(dir);i++ )
{
files = Directory(dir[i]);
for( j=0;files && j<=GetArraySpan(files);j++ )
{
touch_file(files[j]);
}
}
}